<?php
/**
 *
 */
class M_shop extends CI_Model {

  public function __construct() {
    // parent::__construct();
    $this->load->database();
  }
  public function flag_selectShop($cloum,$data){
    if(!$cloum || !$data)return false;
    $sql = 'select * from shops where "'.$cloum.'" like "%'.$data.'%" ';
    $query = $this->db->query($sql);
    if($query->num_rows()>0){
      return true;
    }else{
      return false;
    }
  }
  /**
  *关键字查找店铺 $selectkey:关键字 $count,$offset
  *return :shoplist,total
  */
  public function selectShop($selectkey,$count,$offset){
    $sql = ' select * from shops ';
    if(!empty($selectkey)){
      $selectkey = '"%'.$selectkey.'%"';
      $sql = $sql."where ".
      'shopname like '.$selectkey.' or '.
      'shopid like '.$selectkey.' or '.
      'shopboss like '.$selectkey.' or '.
      'shoptel like '.$selectkey.' or '.
      'shopbrand like '.$selectkey.' or '.
      'shopotherbrand like '.$selectkey.' or '.
      'shopinfo like '.$selectkey.' or '.
      'shopconsignee like '.$selectkey.' or '.
      'shopctel like '.$selectkey.' or '.
      'shopaddr like '.$selectkey.
      ' order by shopid ';
      $total = $this->db->query($sql);
      $sql = $sql.'limit '.$offset.','.$count;
      $query = $this->db->query($sql);
      return array(
        'shoplist' => $query->result_array(),
        'total'    => $total->num_rows(),
        'totallist'=> $total->result_array()
      );
    }else{
      return false;
    }
  }
  /**
  *获取店铺详情 && 回访记录
  */
  public function getShopDetail($shopid,$count,$offset){
    $sql = 'select * from shops where shopid='.$shopid;
    $shop =$this->db->query($sql);
    $callback = $this->db->query('select shop_callback.id,shopid,shopcallback,addtime,emname as author from shop_callback,employees where author = emid AND shopid = '.$shopid.' UNION ALL select shop_callback.* FROM shop_callback WHERE author = "admin" AND shopid = '.$shopid.' order by addtime desc limit '.$offset.','.$count);
    $totalcallback = $this->db->query('select * from shop_callback,employees where shopid='.$shopid);
    return array(
      'shopdetail' => $shop->result_array(),
      'flag'       => $shop->num_rows(),
      'callback'   => $callback->result_array(),
      'total'      => $totalcallback->num_rows(),
      'totallist'  => $totalcallback->result_array()
    );
  }
  /**
  *获取店铺列表 $count:每页显示数量 $offset:开始位置
  *return shoplist:店铺列表 total:总数量
  */
  public function getShopList($count,$offset){
    $sql = ' select * from shops where shopid!=0 order by shopid limit '.$offset.','.$count;
    $query = $this->db->query($sql);
    $total = $this->db->query('select * from shops');
    return array(
      'shoplist' => $query->result_array(),
      'total'    => $total->num_rows(),
      'totallist'=> $total->result_array()
    );
  }
  /**
  *添加店铺回访记录
  */
  public function addShopCallback($shopid,$val,$author){
    $sql = 'insert into shop_callback(shopid,shopcallback,addtime,author) values('.$shopid.',"'.$val.'","'.date("Y-m-d H:i:s").'","'.$author.'")';
    // return $sql;
    return $this->db->simple_query($sql);
  }
  /**
  *增加店铺
  */
  public function addshop($shop){
    $sql='insert into shops(shopname,shopboss,shoptel,shopbrand,shopotherbrand,shopinfo,shopconsignee,shopctel,shopaddr)'.
    ' values("'.$shop['shopname'].'","'.$shop['shopboss'].'","'.$shop['shoptel'].'","'.$shop['shopbrandname'].'","'.$shop['shopotherbrand'].'","'.$shop['shopinfo'].'","'
    .$shop['shopconsignee'].'","'.$shop['shopctel'].'","'.$shop['shopaddr'].'")';
    $this->db->simple_query($sql);
    $reid = $this->db->query('select max(shopid) from shops')->result_array();
    $bid = explode(",", $shop['shopbrand']);
    for ($i=0; $i < sizeof($bid) ; $i++) {
      $ssql = 'select * from brandshop shopid='.$shopid.'and bid='.$bid[$i];
      if('selec')
      $this->db->simple_query('insert into brandshop (shopid,bid) values('.$reid[0]['max(shopid)'].','.$bid[$i].')');
    }
    return '1';
  }
  /**
  *修改店铺信息
  */
  public function updateShop($shopid,$option){
    if(!$shopid || !$option)return false;
    $oshopname = $this->db->query('select shopname from shops where shopid='.$shopid)->result_array();
    $oshopname = $oshopname[0]['shopname'];
    $sql = ' update shops set shopname="'.$option['shopname'].
    '",shopboss="'.$option['shopboss'].
    '",shoptel="'.$option['shoptel'].
    '",shopbrand="'.$option['shopbrandname'].
    '",shopinfo="'.$option['shopinfo'].
    '",shopotherbrand="'.$option['shopotherbrand'].
    '",shopconsignee="'.$option['shopconsignee'].
    '",shopctel="'.$option['shopctel'].
    '",shopaddr="'.$option['shopaddr'].
    '" where shopid='.$shopid;
    $this->db->simple_query($sql);
    $this->db->simple_query('delete from brandshop where shopid='.$shopid);
    $bid = explode(",", $option['shopbrand']);
    for ($i=0; $i < sizeof($bid) ; $i++) {
      $this->db->simple_query('insert into brandshop (shopid,bid) values('.$shopid.','.$bid[$i].')');
    }

    $result = $this->db->query('select * from teshop where shopid='.$shopid)->result_array();
    foreach ($result as $terminal):
      $re = $this->db->query('select shopid from terminal where teid='.$terminal['teid'])->result_array();
      $b = explode(',',$re[0]['shopid']);
      $str = '';
      for ($i=0; $i <sizeof($b) ; $i++) {
        if($b[$i]==$oshopname){
          $b[$i]=$option['shopname'];
        }
      }
      for ($i=0;$i<sizeof($b);$i++){
        if($i==0){
          $str = $str.$b[$i];
        }else{
          $str = $str.','.$b[$i];
        }
      }
      $this->db->simple_query('update terminal set shopid = "'.$str.'" where teid='.$terminal['teid']);
    endforeach;
    return '1';
  }
  public function deleteShop($shopid){
    if(!$shopid)return false;
    $sql = 'delete from shops where shopid='.$shopid;
    $oshopname = $this->db->query('select shopname from shops where shopid='.$shopid)->result_array();
    $oshopname = $oshopname[0]['shopname'];
    $result = $this->db->query('select * from teshop where shopid='.$shopid)->result_array();
    foreach ($result as $terminal):
      $re = $this->db->query('select shopid from terminal where teid='.$terminal['teid'])->result_array();
      $b = explode(',',$re[0]['shopid']);
      $str = '';
      for ($i=0; $i <sizeof($b) ; $i++) {
        if($b[$i]==$oshopname){
          array_splice($b,$i,1);
        }
      }
      for ($i=0;$i<sizeof($b);$i++){
        if($i==0){
          $str = $str.$b[$i];
        }else{
          $str = $str.','.$b[$i];
        }
      }
      $this->db->simple_query('update terminal set shopid = "'.$str.'" where teid='.$terminal['teid']);
    endforeach;
    $this->db->simple_query('delete from summary where shopid='.$shopid);
    $this->db->simple_query('delete from teshop where shopid='.$shopid);
    $this->db->simple_query('delete from brandshop where shopid='.$shopid);
    return $this->db->simple_query($sql);
  }
  /**
  *修改回访记录
  */
  public function updataCallback($id,$val){
    $sql = 'update shop_callback set shopcallback="'.$val.'"'.' where id='.$id;
    return $this->db->simple_query($sql);
  }
  /**
  *删除回访记录
  */
  public function deleteCallback($id){
    $sql = 'delete from shop_callback where id='.$id;
    return $this->db->simple_query($sql);
  }
  /**
  *查看自己的回访记录
  */
  public function getMycallback($uid,$count,$offset){
    $callback = $this->db->query('select shop_callback.*,shopname from shop_callback,shops where shop_callback.shopid=shops.shopid and author='.$uid.' order by addtime desc limit '.$offset.','.$count);
    $totalcallback = $this->db->query('select shop_callback.*,shopname from shop_callback,shops where shop_callback.shopid=shops.shopid and author='.$uid);
    return array(
      'callback'   => $callback->result_array(),
      'total'      => $totalcallback->num_rows(),
      'totallist'  => $totalcallback->result_array()
    );
  }
  /**
  *查询回访记录
  */
  public function selectMycallback($selectkey,$uid,$count,$offset){
    // $callback = $this->db->query('select shop_callback.*,shopname from shop_callback,shops where shop_callback.shopid=shops.shopid and author='.$uid.' order by addtime desc limit '.$offset.','.$count);
    $sql = 'select shop_callback.*,shopname from shop_callback,shops ';
    // ' order by addtime desc limit '.$offset.','.$count
    if(!empty($selectkey)){
      $selectkey = '"%'.$selectkey.'%"';
      $sql = $sql." where ".
      '(shopname like '.$selectkey.' or '.
      'shopcallback like '.$selectkey.' ) '.
      'and shop_callback.shopid=shops.shopid and author='.$uid.
      ' order by addtime desc ';
      $total = $this->db->query($sql);
      $sql = $sql.'limit '.$offset.','.$count;
      $query = $this->db->query($sql);
      // return $sql;
      return array(
        'callback' => $query->result_array(),
        'total'    => $total->num_rows(),
        'totallist'=> $total->result_array()
      );
    }else{
      return false;
    }
  }
  /**
  *查询回访记录
  */
  public function getallshop($count,$offset){
    $sql = 'select shopid,shopname from shops where shopid!=0 limit '.$offset.','.$count;
    $total = $this->db->query('select shopid,shopname from shops');
    return array(
      'shoplist' => $this->db->query($sql)->result_array(),
      'total'    => $total->num_rows()
    );
    // return $this->db->query($sql)->result_array();
  }
  public function getallshopselect(){
    $sql = 'select shopid,shopname from shops';
    return $this->db->query($sql)->result_array();
  }
  public function getbrandtecount($shopid,$count,$offset){
    $sql = 'select shopname,addday,bname,tecount from shops,brandshop,brand,summary where brandshop.shopid=shops.shopid and brandshop.shopid='.$shopid.' and brandshop.bid=brand.bid and brandshop.bid=summary.bid and brandshop.shopid=summary.shopid order by addday limit '.$offset.','.$count;
    return array(
      'result' => $this->db->query($sql)->result_array(),
      'row'    => $this->db->query($sql)->num_rows()
     );
  }
}
